package com.unieagle.exam.ui;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.unieagle.exam.R;
import com.unieagle.exam.db.DatabaseContext;
import com.unieagle.exam.db.ExamDBHelper;
import com.unieagle.exam.model.Subject;
import com.unieagle.exam.utils.SharedPreferenceUtils;

/**
 * 二级科目选择
 * @author QianJing
 *
 */
public class SubjectChoose2Activity extends BaseActivity{
	
	private List<Subject> subjects;
	
	private ListView mListView;
	
	private SubjectAdapter mAdapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.subject_choose2_activity_layout);
		init();
	}
	
	protected void init() {
		super.init();
		mTvTitle.setText(getIntent().getStringExtra("title"));
		mListView = (ListView) findViewById(R.id.list);
		getSubjects();
	}
	
	private void getSubjects() {
		subjects = new ArrayList<Subject>();
		new GetSubjectsTask().execute();
	}
	
	/**
	 * 获取工种
	 * @author QianJing
	 *
	 */
	class GetSubjectsTask extends AsyncTask<Void, Void, Boolean> {

		@Override
		protected Boolean doInBackground(Void... arg0) {
			ExamDBHelper helper = new ExamDBHelper(new DatabaseContext(SubjectChoose2Activity.this));
			subjects = helper.getSubject2(getIntent().getIntExtra("id", -1));
			if(subjects.size() > 0) {
				return true;
			}
			return false;
		}

		@Override
		protected void onPostExecute(Boolean result) {
			super.onPostExecute(result);
			if(result) {
				mAdapter = new SubjectAdapter(SubjectChoose2Activity.this, subjects);
				mListView.setAdapter(mAdapter);
			}
		}
		
	}
	
	private void startPreExam(int id, String title1, String title2) { //选择二级科目
		Intent i = new Intent(this, PreExamActivity.class);
		i.putExtra("id", id);
		i.putExtra("title1", title1);
		i.putExtra("title2", title2);
		startActivity(i);
	}
	
	class SubjectAdapter extends BaseAdapter {
		
		private List<Subject> subjects;
		
		private LayoutInflater inflator;
		
		public SubjectAdapter(Context ctx, List<Subject> subjects) {
			this.subjects = subjects;
			inflator = LayoutInflater.from(ctx);
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return subjects.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return subjects.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		@Override
		public View getView(int arg0, View convertView, ViewGroup arg2) {
			// TODO Auto-generated method stub
			TextView mTvSubject;
			if (convertView == null) {
				convertView = inflator.inflate(R.layout.subject_choose2_activity_listitem, null);
				mTvSubject = (TextView) convertView.findViewById(R.id.subject);
				convertView.setTag(mTvSubject);
			}else {
				mTvSubject = (TextView) convertView.getTag();
			}
			Subject sub = subjects.get(arg0);
			final int id = (int)sub.getId();
			final String title = sub.getTitle();
			mTvSubject.setText(title);
			convertView.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View arg0) {
					int cnt = SharedPreferenceUtils.getFreeExamCount(SubjectChoose2Activity.this);
					if(cnt > 0) { //还剩余试用次数
						startPreExam(id, getIntent().getStringExtra("title"), title);
					} else {
						Toast.makeText(SubjectChoose2Activity.this, "5次试用已满，请申请激活", Toast.LENGTH_SHORT).show();
					}
				}
			});
			return convertView;
		}
		
	}
	
}
